package com.beerandjesus.webapp.sportofkings.handicapper.facade;

import com.beerandjesus.sportofkings.handicapper.model.JockeyRatingThreshold;
import com.beerandjesus.sportofkings.handicapper.model.JockeyRatingThresholdImpl;
import com.beerandjesus.sportofkings.handicapper.dao.Database;
import com.beerandjesus.sportofkings.handicapper.dao.aspect.TransactionBoundary;

import java.util.List;
import java.math.BigDecimal;

/**
 * Date: Sep 30, 2012
 * Time: 4:06:31 PM
 */
public class TransactionFacadeImpl implements TransactionFacade {
    private Database database;

    @TransactionBoundary(datasources = Database.Datasource.JOCKEY,
            expectedExceptions = {RatingThresholdsException.class})
    public List<JockeyRatingThreshold> doRatingThresholdsAction(RatingThresholdsVO vo)
            throws RatingThresholdsException {
        List<JockeyRatingThreshold> thresholds;
        RatingThresholdsException e;
        JockeyRatingThresholdImpl t = null;
        if (vo != null) {
            if (!empty(vo.getName()) ||
                    !empty(vo.getLeading())||
                    !empty(vo.getExcellent()) ||
                    !empty(vo.getGood())) {
                if (empty(vo.getName()) ||
                    empty(vo.getLeading()) ||
                    empty(vo.getExcellent()) ||
                    empty(vo.getGood())) {
                    thresholds = getDatabase().getAllJockeyRatingThresholds();
                    e = new RatingThresholdsException("All fields are required for new jockey rating thresholds");
                    e.setResult(thresholds);
                    throw e;
                }
                t = new JockeyRatingThresholdImpl();
                t.setActive(true);
                t.setExcellent(new BigDecimal(vo.getExcellent()));
                t.setGood(new BigDecimal(vo.getGood()));
                t.setLeading(new BigDecimal(vo.getLeading()));
                t.setName(vo.getName());
            }
        }
        if (t != null) {
            getDatabase().save(t);
        }
        thresholds = getDatabase().getAllJockeyRatingThresholds();
        if (vo != null && vo.getDeleteThreshold() != null && vo.getDeleteThreshold().length > 0) {
            for (String toDelete : vo.getDeleteThreshold()) {
                for (JockeyRatingThreshold threshold : thresholds) {
                    if (threshold.getName().equals(toDelete)) {
                        getDatabase().delete(threshold);
                    }
                }
            }
        }
        if (vo != null && vo.getActiveThreshold() != null) {
            for (JockeyRatingThreshold threshold : thresholds) {
                if (threshold.getName().equals(vo.getActiveThreshold()) &&
                        !threshold.getActive()) {
                    threshold.setActive(true);
                    getDatabase().save(threshold);
                }
            }
        }
        return thresholds;
    }

    private boolean empty(String s) {
        return s == null || s.equals("");
    }
    private boolean empty(Double d) {
        return d == null || d.equals(0d);
    }

    public Database getDatabase() {
        return database;
    }

    public void setDatabase(Database database) {
        this.database = database;
    }
}
